package top.jle.spring.basic.api;

import cn.hutool.core.io.resource.NoResourceException;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import top.jle.jpa.basic.exception.BasicCustomException;
import top.jle.jpa.basic.exception.BasicExceptionCode;
import top.jle.jpa.basic.util.ExceptionUtils;

import java.io.Serializable;
import java.util.Arrays;


@RestControllerAdvice
public class APIExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(APIExceptionHandler.class);
    @ExceptionHandler({BasicCustomException.class})
    public ResponseBack<Serializable> businessExceptionHandler(BasicCustomException e) {
        LOGGER.error(e.getMessage(),e);
        return ResponseBack.error(e);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public void handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletResponse response) {
        LOGGER.error(e.getMessage(),e);
        response.setStatus(HttpStatus.NOT_FOUND.value());
        response.setContentType(MediaType.TEXT_HTML_VALUE);
    }
    @ExceptionHandler({BindException.class,MethodArgumentNotValidException.class})
    public ResponseBack<String> bindExceptionHandler(BindException e){
        LOGGER.error(e.getMessage(),e);
        ResponseBack<String> back = ResponseBack.error(e);
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        back.setCode(BasicExceptionCode.INVALID_PARAM.getCode());
        back.setMessage(objectError.getDefaultMessage());
        return back;
    }
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResponseBack<String> methodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e){
        LOGGER.error(e.getMessage(),e);
        ResponseBack<String> back = ResponseBack.error(e);
        back.setCode(String.valueOf(HttpStatus.METHOD_NOT_ALLOWED.value()));
        back.setMessage("当前接口支持的Method为"+ Arrays.toString(e.getSupportedMethods()));
        return back;
    }

    @ExceptionHandler({MissingServletRequestParameterException.class,HttpMessageNotReadableException.class})
    public ResponseBack<String> missingServletRequestParameterExceptionHandler(Exception e){
        LOGGER.error(e.getMessage(),e);
        return ResponseBack.error(ExceptionUtils.buildForInvalidParam().get());
    }
    @ExceptionHandler({Exception.class})
    public ResponseBack<String> bindExceptionHandler(Exception e){
        LOGGER.error(e.getMessage(),e);
        ResponseBack<String> back = ResponseBack.error(e);
        back.setCode(BasicExceptionCode.SERVER_ERROR.getCode());
        back.setMessage(e.getMessage());
        return back;
    }
    @ExceptionHandler({NoResourceException.class, NoResourceFoundException.class})
    public void bindExceptionHandler(Exception e,HttpServletResponse response){
        LOGGER.error(e.getMessage(),e);
        response.setStatus(HttpStatus.NOT_FOUND.value());
        response.setContentType(MediaType.TEXT_HTML_VALUE);
    }
}
